package com.lzh.others.collection.Map.entity;

import com.fasterxml.jackson.annotation.JsonIncludeProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.StringJoiner;

/**
 * OBD信息
 *
 * @author lzh
 */
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
@JsonIncludeProperties({"OBDZDXY", "GZMZS", "MIL", "GZMLB"})
public class ObdInfoEntity {
    private String id;
    /**
     * obd诊断协议
     */
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY, value = "OBDZDXY")
    private String diagnosticProtocol;
    /**
     * 故障码总数
     */
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY, value = "GZMZS")
    private String faultCodesTotalNum;
    /**
     * mil状态
     */
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY, value = "MIL")
    private Boolean milStatus;
    /**
     * 故障码信息
     */
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY, value = "GZMLB")
    private String faultCodeInfo;

    /**
     * 有效范围0～1，“0”代表未点亮，“1”代表点亮
     *
     * @param milStatus mil状态码
     */
    public void setMilStatus(String milStatus) {
        if (milStatus == null) this.milStatus = null;
        if ("0".equals(milStatus)) this.milStatus = false;
        if ("1".equals(milStatus)) this.milStatus = true;
    }

    /**
     * false:未点亮;true:点亮
     *
     * @return 未点亮/点亮
     */
    public String getMilStatus() {
        if (milStatus == null) {
            return "—";
        }
        if (Boolean.FALSE.equals(milStatus)) {
            return "未点亮";
        }
        return "点亮";
    }

    /**
     * 有效范围0～2，“0”代表ISO15765，“1”代表ISO27145，“2” 代表SAEJ1939
     *
     * @param diagnosticProtocol
     */
    public void setDiagnosticProtocol(String diagnosticProtocol) {
        if (diagnosticProtocol == null) {
            return;
        }
        if ("0".equals(diagnosticProtocol)) this.diagnosticProtocol = "ISO15765";
        if ("1".equals(diagnosticProtocol)) this.diagnosticProtocol = "ISO27145";
        if ("2".equals(diagnosticProtocol)) this.diagnosticProtocol = "SAEJ1939";
    }

    /**
     * * 每个故障码为四个字节，可按故障实际顺序进行排序。
     * * 故障码信息列表长度为故障码总数*4；当故障码总数为0或无效时，故障码信息列表可为空。
     *
     * @param faultCodeInfo
     */
    public void setFaultCodeInfo(String faultCodeInfo) {
        if (faultCodeInfo == null) return;

        StringJoiner stringJoiner = new StringJoiner(",");
        int length = faultCodeInfo.length() / 4;
        int remainder = faultCodeInfo.length() % 4;
        if (remainder != 0) {
            length += 1;
        }
        for (int i = 0; i < length; i++) {
            if (remainder != 0 && i == (length - 1)) {
                stringJoiner.add(faultCodeInfo.substring(i * 4, i * 4 + remainder));
            } else {
                stringJoiner.add(faultCodeInfo.substring(i * 4, (i + 1) * 4));
            }
        }
        this.faultCodeInfo = stringJoiner.toString();
    }

}
